package com.fcy.teamwork.business.main.homepage.viewmodel

import androidx.lifecycle.ViewModel
import com.fcy.teamwork.pojo.JobDetail
import com.fcy.teamwork.repo.RetrofitClient

class TaskMarketVM : ViewModel() {
    private val service: TaskMarketService by lazy { RetrofitClient.getService() }

    companion object {
        private const val pageSize = 40
        private var pageNo = 1
    }

    suspend fun loadJobs(): List<JobDetail> {
        val workResult = service.loadJobs(pageNo, pageSize)
        return if (workResult.isOk) {
            val arrayList = ArrayList<JobDetail>()
            arrayList.addAll(workResult.data)
            arrayList
        } else {
            emptyList()
        }
    }

    suspend fun insertNextPage(): List<JobDetail> {
        val workResult = service.loadJobs(++pageNo, pageSize)
        return if (workResult.isOk && workResult.data.isNotEmpty()) {
            workResult.data
        } else {
            --pageNo
            emptyList()
        }

    }

}